package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.IDistrictService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private IDistrictService iDistrictService;

    @Value("${project.max-count}")
    private Integer maxCount;

    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        //调用收货地址统计的方法
        Integer count = addressMapper.countByUid(uid);
        if(count>=maxCount){
            throw new AddressCountLimitException("用户地址超出上限");
        }

        //对address对象中数据进行补全：省市区·
        String ProvinceName =  iDistrictService.getNameByCode(address.getProvinceCode());
        String CityName =  iDistrictService.getNameByCode(address.getCityCode());
        String AreaName =  iDistrictService.getNameByCode(address.getAreaCode());
        address.setProvinceName(ProvinceName);
        address.setCityName(CityName);
        address.setAreaName(AreaName);

        //uid isDefault
        address.setUid(uid);
        Integer isDefault = count == 0 ? 1 : 0;
        address.setIsDefault(isDefault);

        //补全日志
        address.setCreatedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());

        //插入收获地址
        Integer len = addressMapper.insert(address);
        if(len!=1){
            throw new InsertException("插入用户收货地址产生未知异常");
        }

    }

    @Override
    public List<Address> getByUid(Integer uid) {
        List<Address> list = addressMapper.findByUid(uid);
        for (Address address : list){
//            address.setAid(null);
//            address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setZip(null);
            address.setTel(null);
            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        return list;
    }

    @Override
    public void setDefault(Integer uid, Integer aid, String username) {
        Address result = addressMapper.findByAid(aid);
        if(result==null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if(!uid.equals(result.getUid())){
            throw new AccessDeniedException("非法数据访问");
        }

        //将所有收货地址设置为非默认
        Integer len = addressMapper.updateNonDefault(uid);
        if(len<1){
            throw new UpdateException("更新数据产生未知异常");
        }

        //将用户选中某条地址改为默认地址
        Integer row = addressMapper.updateDefaultByAid(aid,username,new Date());
        if(row!=1){
            throw new UpdateException("更新数据产生未知异常");
        }

    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address address = addressMapper.findByAid(aid);
        if(address==null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if(!uid.equals(address.getUid())){
            throw new AccessDeniedException("非法数据访问");
        }

        Integer len = addressMapper.deleteByAid(aid);
        if(len!=1){
            throw new DeleteException("删除数据产生未知异常");
        }

        if(address.getIsDefault()==1){
            Integer count = addressMapper.countByUid(uid);
            if(count==0){
                return;
            }

            Address result = addressMapper.findLastModified(uid);

            Integer row = addressMapper.updateDefaultByAid(result.getAid(),username,new Date());

            if(row!=1){
                throw new UpdateException("更新时发生未知异常");
            }
        }


    }

    @Override
    public Address getByAid(Integer aid,Integer uid) {
        Address address = addressMapper.findByAid(aid);
        if(address==null){
            throw new AddressNotFoundException("收货数据不存在");
        }

        if(address.getUid()!=uid){
            throw new AccessDeniedException("不合法操作");
        }

        address.setProvinceCode(null);
        address.setCityCode(null);
        address.setAreaCode(null);
        address.setModifiedUser(null);
        address.setModifiedTime(null);
        address.setCreatedUser(null);
        address.setCreatedTime(null);

        return address;

    }
}
